package com.lhn.bbs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhn.bbs.dto.ActionResult;
import com.lhn.bbs.mapper.ReplyDao;
import com.lhn.bbs.mapper.UserDao;
import com.lhn.bbs.service.ReplyService;
import com.lhn.bbs.utils.SensitiveFilter;
import com.lhn.bbs.vo.ReplyAddVo;
import com.lhn.bbs.vo.ReplyThemeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReplyServiceImpl implements ReplyService {

    @Autowired(required = false)
    private ReplyDao replyDao ;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired(required = false)
    private UserDao userDao ;


    /**
     * 主题帖的评论回复
     * @param replyAddVo
     * @return
     */
    @Override
    public ActionResult replyadd(ReplyAddVo replyAddVo) {

        try{
            // 转义HTML标记
            replyAddVo.setReplytext(HtmlUtils.htmlEscape(replyAddVo.getReplytext()));
            // 过滤敏感词
            replyAddVo.setReplytext(sensitiveFilter.filter(replyAddVo.getReplytext()));

            long  l = replyDao.saveReply(replyAddVo);
            log.info("==========="+l);

            return new ActionResult(200,"添加成功",null,null);
        }catch (Exception e){
            log.info("========="+e.getMessage());
            return new ActionResult(201,"添加失败",null,null);
        }

    }

    /**
     * 查询我的回复帖
     * @param userid
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ActionResult replythemeByUserid(Integer userid, Integer page, Integer limit) {
           PageHelper.startPage(page,limit);
           List<ReplyThemeVo> replyThemeVos = replyDao.findReplyByToid(userid);
           // 查询我的回复帖子的数量
           Integer replynumber = replyDao.findReplyCountByUserid(userid);
           userDao.editreplynum(replynumber,userid);

           if(replyThemeVos != null) {
               PageInfo<ReplyThemeVo> pageInfo = new PageInfo(replyThemeVos);
               long total = pageInfo.getTotal();
               Map<String,Object> map = new HashMap<>();

               map.put("replynumber",replynumber);
               map.put("reply",pageInfo);
               return new ActionResult(200, "success", map, total);
           }
           log.info("-------replythemeByUserid方法出错了");
           return new ActionResult(201,"查询失败",null,null);

    }

    /**
     * 根据id删除reply表中的内容
     * @param replyid
     * @return
     */
    @Override
    public ActionResult deleteReplyByid(Integer replyid) {

        try{
            Boolean b  = replyDao.deleteByIdReply(replyid);

            return new ActionResult(200,"删除成功",null,null);
        }catch (Exception e){
            log.info("=======reply删除失败"+e.getMessage());
            return new ActionResult(201,"删除失败",null,null);
        }
    }


}
